package com.hssy.wululuaplan.controller.KeepAccount;

import com.hssy.wululuaplan.common.Result;
import com.hssy.wululuaplan.entity.KeepAccount;
import com.hssy.wululuaplan.form.AccountForm;
import com.hssy.wululuaplan.service.KeepAccountService;
import com.hssy.wululuaplan.vo.account.ChartVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

@Api(tags = "账务管理")
@RestController
@RequestMapping("/keepAccount")
public class AccountController {
    @Resource
    private KeepAccountService keepAccountService;

    @PostMapping("/addAccount")
    public Result<String> addAccount(@RequestBody AccountForm form, HttpServletRequest request) {
        // 添加账单
        ReentrantLock lock = new ReentrantLock();
        lock.lock();
        try {
            keepAccountService.addAccount(form, request);
        } finally {
            lock.unlock();
        }
        return Result.success("添加账单成功");
    }

    @GetMapping("/getStatistic")
    public Result<Map<String, BigDecimal>> getStatistic(@RequestParam(value = "date",required = false) String date, HttpServletRequest request) {
        Map<String, BigDecimal> statistic = keepAccountService.getStatistic(date, request);
        return Result.success(statistic);
    }


    @GetMapping("getList")
    public Result<List<KeepAccount>> getList(@RequestParam("dateType") String dateType, @RequestParam("date")String date,  HttpServletRequest request) {
        List<KeepAccount> recordList = keepAccountService.getRecordList(dateType, date,request);
        return Result.success(recordList);
    }

    @ApiOperation("获取数据曲线")
    @GetMapping("getChartDataList")
    public Result<List<ChartVo>> getChartDataList(@RequestParam("dateType")String dateType, @RequestParam("date")String date, HttpServletRequest request) {
        List<ChartVo> recordList = keepAccountService.getChartDataList(dateType, date,request);
        return Result.success(recordList);
    }


}
